from datetime import timedelta
import numpy as np
from os import mkdir
from os.path import isfile, dirname, basename
from utils.read_micaps import read_micaps_4
from utils.config import *


def get_time(data_time, days=0, hours=0):
    if not isinstance(data_time, datetime):
        data_time = datetime.strptime(str(data_time)[:10], '%Y%m%d%H')
    new_time = (data_time + timedelta(days=int(days), hours=int(hours))).strftime('%Y%m%d%H')
    return new_time


def get_scmoc(path, datatime, hour_step):
    scmoc_file = f"{path}/{datatime[2:10]}.{str(hour_step).rjust(3, '0')}"
    if not isfile(scmoc_file):
        return None
    return read_micaps_4(scmoc_file)


def get_revise(path, datatime, hour_step):
    revise_file = f"{path}/{datatime[2:10]}.{str(hour_step).rjust(3, '0')}"

    if not isfile(revise_file):
        return None

    return read_micaps_4(revise_file)


def get_cldas(path, start_time, hour_step):
    fst_time = get_time(start_time, hours=hour_step)
    cldas_path = pathjoin(path, fst_time[2:10] + '.npy')

    if not isfile(cldas_path):
        return None

    return np.load(cldas_path)


if __name__ == '__main__':
    pass
    # get_sbc_data()
